import CryptoJS from 'crypto-js';

export function generateSign(secret: any, params: Record<string, any>) {
  let fullParamStr = uniqSortParams(params);
  return hmacSHA256(fullParamStr, secret);
}

function uniqSortParams(params: Record<string, any>) {
  delete params.sign;

  let var5 = [];
  let var6 = 0;
  for (let key in params) {
    var5[var6] = key;
    var6++;
  }
  var5.sort(function (a, b) {
    return a.localeCompare(b, 'zh-CN');
  });

  let result = '';
  for (let var7 = 0; var7 < var5.length; var7++) {
    let key = var5[var7];
    let var8 = params[key];
    var8.sort(function (a: any, b: any) {
      return a.localeCompare(b, 'zh-CN');
    });

    if (var8 !== null && var8.length > 0) {
      for (let var9 = 0; var9 < var8.length; var9++) {
        result += key + '=' + var8[var9];
      }
    } else {
      result += key + '=';
    }
  }
  return result;
}

function hmacSHA256(data: string, key: string) {
  data !== null ? data : '';
  let var2 = CryptoJS.HmacSHA256(data, key);
  let var3 = var2.toString(CryptoJS.enc.Hex);
  return var3.toUpperCase();
}
